#include "fileextensionmanager.h"
#include "../data/fileplugin.h"

using namespace PBVP;

FileExtensionManager::FileExtensionManager()
{

}

FileExtensionManager::~FileExtensionManager()
{
	FilePluginInterface *pFilePlugin;
	while (m_GeometryPluginList.size() > 0)
	{	
		pFilePlugin = m_GeometryPluginList.last();
		m_GeometryPluginList.pop_back();
		pFilePlugin->unload();
		delete pFilePlugin;
	}
	while (m_VolumePluginList.size() > 0)
	{
		pFilePlugin = m_VolumePluginList.last();
		m_VolumePluginList.pop_back();
		pFilePlugin->unload();
		delete pFilePlugin;
	}
	while (m_ProteinPluginList.size() > 0)
	{
		pFilePlugin = m_ProteinPluginList.last();
		m_ProteinPluginList.pop_back();
		pFilePlugin->unload();
		delete pFilePlugin;
	}
}

void FileExtensionManager::registerPlugin(FilePluginInterface* pFilePlugin)
{
	switch(pFilePlugin->type()) {
	case GEOMETRY_DATA:
		m_GeometryPluginList.push_back(pFilePlugin);
		break;
	case VOLUME_DATA:
		m_VolumePluginList.push_back(pFilePlugin);
		break;
	case PROTEIN_DATA:
		m_ProteinPluginList.push_back(pFilePlugin);
		break;
	default:
		break;
	}
}

QString FileExtensionManager::filter()
{
	QString str = "";
	if (m_GeometryPluginList.size() > 0)
	{
		str.append("Geometry Files (");
		for(int i = 0; i < m_GeometryPluginList.size(); i++) {
			str.append("*");
			str.append(m_GeometryPluginList[i]->extension());
			str.append(" ");
		}
		str.append(");;");
	}
	if (m_VolumePluginList.size() > 0)
	{
		str.append("Volume Files (");
		for(int i = 0; i < m_VolumePluginList.size(); i++) {
			str.append("*");
			str.append(m_VolumePluginList[i]->extension());
			str.append(" ");
		}
		str.append(");;");
	}
	if (m_ProteinPluginList.size() > 0)
	{
		str.append("Protein Files (");
		for(int i = 0; i < m_ProteinPluginList.size(); i++) {
			str.append("*");
			str.append(m_ProteinPluginList[i]->extension());
			str.append(" ");
		}
		str.append(");;");
	}
	str.append("All Files (*.*)");
	return str;
}

QString FileExtensionManager::filter(DataSetType type)
{
	QString str = "";
	switch(type)
	{
	case PBVP::GEOMETRY_DATA:
		for(int i = 0; i < m_GeometryPluginList.size(); i++)
		{
			str.append(m_GeometryPluginList[i]->description());
			str.append("(*");
			str.append(m_GeometryPluginList[i]->extension());
			str.append(");;");
		}
		break;
	case PBVP::VOLUME_DATA:
		for(int i = 0; i < m_VolumePluginList.size(); i++)
		{
			str.append(m_VolumePluginList[i]->description());
			str.append("(*");
			str.append(m_VolumePluginList[i]->extension());
			str.append(");;");
		}
		break;
	case PBVP::PROTEIN_DATA:
		for(int i = 0; i < m_ProteinPluginList.size(); i++)
		{
			str.append(m_ProteinPluginList[i]->description());
			str.append("(*");
			str.append(m_ProteinPluginList[i]->extension());
			str.append(");;");
		}
		break;
	}
	str.append("All Files (*.*)");
	return str;
}

FilePluginInterface* FileExtensionManager::getSupportedPlugin(const QString &fname)
{
	int i;
	for(i = 0; i < m_GeometryPluginList.size(); i++) {
		if(fname.endsWith(m_GeometryPluginList[i]->extension())) {
			return m_GeometryPluginList[i];
		}
	}
	for(i = 0; i < m_VolumePluginList.size(); i++) {
		if(fname.endsWith(m_VolumePluginList[i]->extension())) {
			return m_VolumePluginList[i];
		}
	}
	for(i = 0; i < m_ProteinPluginList.size(); i++) {
		if(fname.endsWith(m_ProteinPluginList[i]->extension())) {
			return m_ProteinPluginList[i];
		}
	}
	return NULL;
}